$(document).ready(
		function() {
			jwplayer("player").setup({
				flashplayer : "/musicai/resources/flash/player.swf",
				height : 487,
				width : 650,
				skin : "/musicai/resources/flash/beelden.zip",
				repeat : 'list',
				'plugins' : 'hd',
				"controlbar": "bottom"
			});
			
			jwplayer('player').onComplete(
					function(event) {
						var playing_node = $(".playing");
						var next_node = playing_node.next();
						$.loadvideo(next_node);
					});

			var keyword = GetQueryString("keyword");
			var pageno = GetQueryString("pageno");
			$.ajax({
				type : "GET",
				url : "/musicai/resultxml",
				beforeSend: function(req) {
					req.setRequestHeader("Accept", "application/application+xml");
				},
				data : {
					qword : keyword,
					page : pageno
				}
			}).done(function(xml) {
				var playlist = [];
				$(xml).find("song_list").each(function() {
					var e = new video();
					e.id = $(this).find("id").text();
					e.title = $(this).find("title").text();
					e.url = $(this).find("url").text();
					e.imagelink = $(this).find("imagelink").text();
					e.duration = $(this).find("duration").text();
					playlist.push(e);
				});
				$.drawlist(playlist);
				$.addclickevent();
//				$.addmouseevent();
				$.autoplay();
				$.addscoreevent();
			});
		});
function GetQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) {
		var x = decodeURI(r[2]);
		return x;
	}
	return null;
}
function video(id, title, url, imagelink, duration) {
	this.id = id;
	this.title = title;
	this.url = url;
	this.imagelink = imagelink;
	this.duration = duration;
}
jQuery.drawlist = function(playlist) {
	for ( var i = 0; i < playlist.length; i++) {
		var html = "<div class=\"playlist_item ";
		if (i % 2 == 0) {
			html += "odd";
		}
		html += "\" id=\"" + playlist[i].id
				+"\"><div class=\"playlist_image_div\"><img alt=\"\" src=\""
				+ playlist[i].imagelink
				+ "\" class=\"playlist_image\"></div><div class=\"playlist_des\">"
				+ playlist[i].title + "</div>"
				+ "<div class=\"control_bar\"><div class=\"score displaynone\"><a>顶[0]</a>|<a>踩[0]</a></div></div><div style=\"display: none\" class=\"playlist_url\">"
				+ playlist[i].url + "</div></div>";
		$("#playlist").append(html);
	}
};
jQuery.addclickevent= function() {
	var playlist_item = $(".playlist_item");
	playlist_item.each(function() {
		$(this).dblclick(function() {
			$.loadvideo($(this));
		});
	});
};
jQuery.loadvideo = function(_onplaynode) {
	var _url = _onplaynode.children(".playlist_url").text();
	var _imagelink = _onplaynode.children(".playlist_image_div").children().attr("src");
	$(".playlist_item").children(".control_bar").children("img").remove();
	_onplaynode.children(".control_bar").append("<img class=\"loadinggif\" src=\"/musicai/resources/img/103.gif\">");
	var st = _onplaynode.index() * 81;
	$("#playlist").scrollTop(st);
	$.ajax({
		type : "GET",
		url : "/musicai/parseurl",
		global : false,
		beforeSend: function(req) {
			req.setRequestHeader("Accept", "application/application+xml");
		},
		data : {
			url : _url,
		}
	}).done(function(xml) {
		var data = $(xml).find("normal").text();
		$(".playlist_item").removeClass("playing");
		_onplaynode.addClass("playing");
		$(".playlist_item").children(".control_bar").children(".playingpnf").remove();
		_onplaynode.children(".control_bar").children("img").remove();
		var title = _onplaynode.children(".playlist_des").text();
		$("#videotitle").html(title);
		$.playingpnf_add(_onplaynode);
		jwplayer().load({
			file : data,
			image : _imagelink,
			'provider':'video'
		});
		jwplayer().play(true);
		var videoid = _onplaynode.attr("id");
		$.loadscore(videoid);
	});
};

jQuery.autoplay = function() {
	var id = GetQueryString("id");
	if(id == null){
		var _onplayingnode = $("#playlist").children().first();
		$.loadvideo(_onplayingnode);
	}else{
		var _onplayingnode = $("#playlist").children("#"+id);
		$.loadvideo(_onplayingnode);
		
	}
};

/**
 * 
 * remove playing image.
 */
jQuery.playingpnf_remove = function(_node) {
	_node.children(".control_bar").children(".playingpnf").remove();
};

/**
 * 
 * add playing image.
 */
jQuery.playingpnf_add = function(_node) {
	_node.children(".control_bar").append("<span class=\"playingpnf\"></span>");
};

jQuery.addmouseevent = function(){
	$(".playlist_item").each(function(){
			var id = $(this).attr("id");
			$(this).mouseout(function(){
				$(this).children(".control_bar").children(".score").addClass("displaynone");
			}).mouseover(function(){
				$(this).children(".control_bar").children(".score").removeClass("displaynone");
			});
	});
};




